package cn.fastoo.common.dao.system;


import cn.fastoo.common.entity.system.AdminSystemDataPermission;
import cn.fastoo.common.entity.system.AdminSystemOrganization;
import cn.fastoo.common.entity.system.UserBaseInfo;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Component
public class AdminSystemDataPermissionDao {

    public Logger logger = LoggerFactory.getLogger(AdminSystemDataPermissionDao.class);


    @Autowired
    private AdminSystemOrganizationDao organizationDao;
    @Autowired
    private UserBaseInfoDao userBaseInfoDao;

    public AdminSystemDataPermission getPermission(Long userId){
        UserBaseInfo userBaseInfo = userBaseInfoDao.findById(userId);
        if(userBaseInfo==null){
            return null;
        }
        AdminSystemDataPermission res = new AdminSystemDataPermission();

        List<AdminSystemOrganization> subOrgan = organizationDao.getChildren(userBaseInfo.getAdminSystemOrganId());
        if(Collections3.isNotEmpty(subOrgan)){
            res.setSubOran(subOrgan);
        }
        AdminSystemOrganization userOrgan = organizationDao.getById(userBaseInfo.getAdminSystemOrganId());
        if(userOrgan.getIsDataSharing().intValue()==0){//如果不是数据共享部门 就加入私有化ID
            res.setPersonalId(userBaseInfo.getId());
        }else{//如果是数据共享部门 就加入此部门ID
            if(subOrgan==null){
                subOrgan = new ArrayList<>(1);
                subOrgan.add(userOrgan);
                res.setSubOran(subOrgan);
            }
        }

        return res;
    }

    public Search addDataPermissionSql (Search search,Long userId) {
        /* 数据权限控制 开始 */
        AdminSystemDataPermission dataPermission = getPermission(userId);
        if (dataPermission == null) {
            return null;
        }
        StringBuilder permissionSql = new StringBuilder();
        if (dataPermission.hasPersonalId()) {
            permissionSql.append(String.format(" personalId=%s ", dataPermission.getPersonalId()));
        }
        if (Collections3.isNotEmpty(dataPermission.getSubOranIds())) {
            if (dataPermission.hasPersonalId()) {
                permissionSql.append(" or ");
            }
            permissionSql.append(String.format("  adminSystemOrganId in (%s) ", StringUtils.join(dataPermission.getSubOranIds(), ",")));
        }
        search.addSearchFilter("SELF_SQL", "(" + permissionSql + ")");
        /* 数据权限控制 结束 */
        return search;
    }
}

